package com.yilian.xlmusic.android.view;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.base.BaseFragment;
import com.yilian.xlmusic.android.utils.CustomAnimation2;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.adapter.FoudMusicAdapter;
import com.yilian.xlmusic.android.view.bean.MusicBean;
import com.yilian.xlmusic.android.view.bean.MusicPlayerBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

public class FoundMusicFragment extends BaseFragment {
    private RecyclerView recycler_view;
    private LinearLayout found_foot;
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_found_music;
    }

    @Override
    public void initBindInject(Object target, View view) {
        recycler_view = view.findViewById(R.id.recycler_view);
        found_foot = view.findViewById(R.id.found_foot);
        getNetResource();

    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isVisibleToUser) {
            //更新适配器的内容
            if(AppConstants.changeItems.size()>0){
                AppConstants.refreshView(AppConstants.changeItems.get(0));
            }
        }
    }


    private void getNetResource() {
        AppConstants.METHOD_NAME = "首页数据：";
        RxHttp.get("/ylmusic-gateway/home")
                .addHeader(AppConstants.ACCESS_TOKEN_KEY, AppConstants.ACCESS_TOKEN)
                .asClass(MusicBean.class)
                .observeOn(AndroidSchedulers.mainThread())  //主线程回调观察者
                .subscribe(result -> {
                    recycler_view.setLayoutManager(new LinearLayoutManager(recycler_view.getContext()));
                    FoudMusicAdapter foudMusicAdapter = new FoudMusicAdapter();
                    List<MusicPlayerBean> musicBeans = new ArrayList<>();
                    //添加'热门推荐'
                    MusicPlayerBean hot_musicBean = new MusicPlayerBean();
                    hot_musicBean.setItem_type(MusicPlayerBean.Tuijian);
                    hot_musicBean.settitle(result.getData().getRecomemnds().getTitle());
                    hot_musicBean.setDataBean(result.getData().getRecomemnds().getList());
                    musicBeans.add(hot_musicBean);
                    //添加'分类'
                    MusicPlayerBean category_musicBean = new MusicPlayerBean();
                    category_musicBean.setItem_type(MusicPlayerBean.MusicCategory);
                    category_musicBean.setCategory(result.getData().getCategories());
                    category_musicBean.settitle("歌单分类");
                    category_musicBean.setDataBean(new ArrayList<>());
                    musicBeans.add(category_musicBean);
                    //添加其他分类
                    for (int i = 0; i < result.getData().getPlayList().size(); i++) {
                        MusicPlayerBean musicPlayerBean = new MusicPlayerBean();
                        musicPlayerBean.setId(result.getData().getPlayList().get(i).getId());
                        musicPlayerBean.settitle(result.getData().getPlayList().get(i).getTitle());
                        musicPlayerBean.setDataBean(result.getData().getPlayList().get(i).getList());
                        musicBeans.add(musicPlayerBean);
                    }
                    foudMusicAdapter.setList(musicBeans);
                    foudMusicAdapter.setAdapterAnimation(new CustomAnimation2());//添加加载动画

                    recycler_view.setAdapter(foudMusicAdapter);


                }, throwable -> {
                    Log.e("请求失败了", "请求失败了" + throwable);
                });
    }
    @Override
    public void init(Bundle savedInstanceState) {

    }


}